<?php
declare(strict_types=1);

namespace SixShop\Message\Controller\Admin;

use think\Request;

/**
 * 消息模板后台控制器
 */
class MessageTemplateController extends BaseController
{
    /**
     * 默认入口方法（兼容资源路由）
     */
    public function index()
    {
        return $this->getList(request());
    }
    
    /**
     * 获取模板列表
     */
    public function getList(Request $request)
    {
        $page = $request->param('page/d', 1);
        $limit = $request->param('limit/d', 10);
        $code = $request->param('code', '');
        $name = $request->param('name', '');
        $type = $request->param('type/d', 0);
        $status = $request->param('status', '');
        
        $params = [];
        if (!empty($code)) {
            $params['code'] = $code;
        }
        if (!empty($name)) {
            $params['name'] = $name;
        }
        if ($type > 0) {
            $params['type'] = $type;
        }
        if ($status !== '') {
            $params['status'] = $status;
        }
        
        $result = $this->messageTemplateModel->getList($params, $page, $limit);
        
        return $this->success('获取成功', $result);
    }
    
    /**
     * 获取模板详情
     */
    public function getDetail(Request $request)
    {
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $detail = $this->messageTemplateModel->getDetail($id);
        if (!$detail) {
            return $this->error('模板不存在');
        }
        
        return $this->success('获取成功', $detail);
    }
    
    /**
     * 添加模板
     */
    public function save(Request $request)
    {
        $code = $request->param('code', '');
        $name = $request->param('name', '');
        $type = $request->param('type/d', 1);
        $title = $request->param('title', '');
        $content = $request->param('content', '');
        $status = $request->param('status/d', 1);
        $remark = $request->param('remark', '');
        
        if (empty($code)) {
            return $this->error('模板代码不能为空');
        }
        
        if (empty($name)) {
            return $this->error('模板名称不能为空');
        }
        
        if (empty($title)) {
            return $this->error('标题模板不能为空');
        }
        
        if (empty($content)) {
            return $this->error('内容模板不能为空');
        }
        
        $data = [
            'code' => $code,
            'name' => $name,
            'type' => $type,
            'title' => $title,
            'content' => $content,
            'status' => $status,
            'remark' => $remark,
        ];
        
        $result = $this->messageTemplateModel->add($data);
        if (!$result) {
            return $this->error('添加失败，模板代码可能已存在');
        }
       
        return $this->success('添加成功', ['id' => $result]);
    }
    
    /**
     * 更新模板
     */
    public function update(Request $request, $id = 0)
    {
        // 确保ID是整数类型
        $id = (int)$id;
        
        // 如果路径参数中的ID无效，尝试从请求参数中获取
        if ($id <= 0) {
            $id = $request->param('id/d', 0);
        }
        
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $code = $request->param('code', '');
        $name = $request->param('name', '');
        $type = $request->param('type/d', null);
        $title = $request->param('title', '');
        $content = $request->param('content', '');
        $status = $request->param('status/d', null);
        $remark = $request->param('remark', '');
        
        $data = [];
        
        if (!empty($code)) {
            $data['code'] = $code;
        }
        
        if (!empty($name)) {
            $data['name'] = $name;
        }
        
        if ($type !== null) {
            $data['type'] = $type;
        }
        
        if (!empty($title)) {
            $data['title'] = $title;
        }
        
        if (!empty($content)) {
            $data['content'] = $content;
        }
        
        if ($status !== null) {
            $data['status'] = $status;
        }
        
        if ($remark !== null) {
            $data['remark'] = $remark;
        }
        
        if (empty($data)) {
            return $this->error('没有需要更新的数据');
        }
        
        $result = $this->messageTemplateModel->update($id, $data);
        if (!$result) {
            return $this->error('更新失败，模板代码可能已存在');
        }
        
        return $this->success('更新成功');
    }
    
    /**
     * 删除模板
     */
    public function delete(Request $request)
    {
        $id = $request->param('id/d', 0);
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->messageTemplateModel->delete($id);
        if (!$result) {
            return $this->error('删除失败');
        }
        
        return $this->success('删除成功');
    }
    
    /**
     * 批量删除模板
     */
    public function batchDelete(Request $request)
    {
        $ids = $request->param('ids');
        if (empty($ids)) {
            return $this->error('参数错误');
        }
        
        if (!is_array($ids)) {
            $ids = explode(',', $ids);
        }
        
        $result = $this->messageTemplateModel->batchDelete($ids);
        if (!$result) {
            return $this->error('删除失败');
        }
        
        return $this->success('删除成功');
    }
    
    /**
     * 更新模板状态
     */
    public function updateStatus(Request $request)
    {
        $id = $request->param('id/d', 0);
        $status = $request->param('status/d', 0);
        
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        
        $result = $this->messageTemplateModel->update($id, ['status' => $status]);
        if (!$result) {
            return $this->error('操作失败');
        }
        
        return $this->success('操作成功');
    }
}
